<template>
  <div class="chinese-zodiac">
    <div class="container">
      <h2>生肖查询</h2>
      
      <div class="content-box">
        <div class="input-section">
          <el-input 
            v-model="yearInput"
            placeholder="请输入出生年份或年龄"
            size="large"
            class="search-input"
            @keyup.enter="handleQuery"
          >
            <template #prefix>
              <el-icon><Calendar /></el-icon>
            </template>
          </el-input>
          <el-button type="primary" size="large" @click="handleQuery">
            <el-icon><Search /></el-icon>
            查询
          </el-button>
        </div>

        <div class="result-section" v-if="showResult">
          <div class="result-card">
            <div class="result-header">
              <div class="zodiac-icon">{{ getZodiacEmoji(result.zodiac) }}</div>
              <div class="zodiac-info">
                <div class="zodiac-title">您的生肖</div>
                <div class="zodiac-value">{{ result.zodiac }}</div>
              </div>
            </div>
            <div class="result-years">
              <div class="years-title">生肖年份</div>
              <div class="years-list">
                <span v-for="(year, index) in result.years" 
                      :key="index" 
                      class="year-tag"
                      :class="{ 'current-year': isCurrentYear(year) }">
                  {{ year }}
                </span>
              </div>
            </div>
          </div>
        </div>

        <div class="zodiac-grid">
          <div v-for="item in zodiacList" 
               :key="item.name" 
               class="zodiac-item"
               :class="{ active: showResult && result.zodiac === item.name }">
            <div class="zodiac-icon">{{ getZodiacEmoji(item.name) }}</div>
            <div class="zodiac-info">
              <div class="zodiac-name">{{ item.name }}</div>
              <div class="zodiac-years">{{ formatYears(item.years) }}</div>
            </div>
          </div>
        </div>

        <div class="help-text">
          <h3>帮助说明：</h3>
          <div class="help-content">
            <p class="help-item">1. 支持两种查询方式：</p>
            <ul>
              <li>输入出生年份（如：1990）</li>
              <li>输入年龄（如：30）</li>
            </ul>
            <p class="help-item">2. 查询范围：1900-2031年</p>
            <p class="help-item">3. 点击下方生肖卡片也可以查看相关年份</p>
          </div>
        </div>

        <div class="back-home">
          <el-button @click="goHome" class="home-button">
            <el-icon><HomeFilled /></el-icon>
            返回首页
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { HomeFilled, Search, Calendar } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const yearInput = ref('')
const showResult = ref(false)
const result = ref({
  zodiac: '',
  years: []
})

const zodiacList = [
  { name: '子鼠', years: '2020, 2008, 1996, 1984, 1972, 1960, 1948, 1936' },
  { name: '丑牛', years: '2021, 2009, 1997, 1985, 1973, 1961, 1949, 1937' },
  { name: '寅虎', years: '2022, 2010, 1998, 1986, 1974, 1962, 1950, 1938' },
  { name: '卯兔', years: '2023, 2011, 1999, 1987, 1975, 1963, 1951, 1939' },
  { name: '辰龙', years: '2024, 2012, 2000, 1988, 1976, 1964, 1952, 1940' },
  { name: '巳蛇', years: '2025, 2013, 2001, 1989, 1977, 1965, 1953, 1941' },
  { name: '午马', years: '2026, 2014, 2002, 1990, 1978, 1966, 1954, 1942' },
  { name: '未羊', years: '2027, 2015, 2003, 1991, 1979, 1967, 1955, 1943' },
  { name: '申猴', years: '2028, 2016, 2004, 1992, 1980, 1968, 1956, 1944' },
  { name: '酉鸡', years: '2029, 2017, 2005, 1993, 1981, 1969, 1957, 1945' },
  { name: '戌狗', years: '2030, 2018, 2006, 1994, 1982, 1970, 1958, 1946' },
  { name: '亥猪', years: '2031, 2019, 2007, 1995, 1983, 1971, 1959, 1947' }
]

const handleQuery = () => {
  const input = yearInput.value.trim()
  if (!input) {
    ElMessage.warning('请输入年份或年龄')
    return
  }

  const currentYear = new Date().getFullYear()
  let year = parseInt(input)

  // 验证输入
  if (isNaN(year)) {
    ElMessage.warning('请输入有效的数字')
    return
  }

  // 如果输入的是年龄,转换为年份
  if (year < 150) {
    year = currentYear - year
  }

  // 验证年份范围
  if (year < 1900 || year > 2031) {
    ElMessage.warning('年份超出范围（1900-2031）')
    return
  }

  // 计算生肖
  const zodiacIndex = (year - 1900) % 12
  if (zodiacIndex < 0 || zodiacIndex >= zodiacList.length) {
    ElMessage.error('计算生肖时出错')
    return
  }

  const zodiacInfo = zodiacList[zodiacIndex]
  if (!zodiacInfo) {
    ElMessage.error('未找到对应的生肖信息')
    return
  }

  result.value = {
    zodiac: zodiacInfo.name,
    years: zodiacInfo.years.split(', ')
  }
  showResult.value = true
}

const goHome = () => {
  router.push('/')
}

// 添加获取生肖表情的方法
const getZodiacEmoji = (zodiac) => {
  const emojis = {
    '子鼠': '🐭',
    '丑牛': '🐮',
    '寅虎': '🐯',
    '卯兔': '🐰',
    '辰': '🐲',
    '巳蛇': '🐍',
    '午马': '🐴',
    '未羊': '🐑',
    '申猴': '🐵',
    '酉鸡': '🐔',
    '戌狗': '🐶',
    '亥猪': '🐷'
  }
  return emojis[zodiac] || ''
}

// 添加判断当前年份的方法
const isCurrentYear = (year) => {
  return year === new Date().getFullYear().toString()
}

// 添加格式化年份的方法
const formatYears = (years) => {
  return years.split(', ').map(year => {
    if (isCurrentYear(year)) {
      return `${year}(今年)`
    }
    return year
  }).join('、')
}
</script>

<style lang="scss" scoped>
.chinese-zodiac {
  padding: 20px;
  max-width: 1000px;
  margin: 0 auto;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #f7f9fc 100%);

  h2 {
    text-align: center;
    color: #2c3e50;
    margin-bottom: 40px;
    font-size: 32px;
    font-weight: 600;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
  }

  .content-box {
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.1);
    padding: 30px;
    transition: all 0.3s ease;

    .input-section {
      max-width: 600px;
      margin: 0 auto 30px;
      display: flex;
      gap: 15px;

      .search-input {
        flex: 1;
        
        :deep(.el-input__wrapper) {
          box-shadow: none;
          border: 2px solid #ebeef5;
          transition: all 0.3s ease;

          &:hover {
            border-color: #409eff;
          }

          &.is-focus {
            border-color: #409eff;
            box-shadow: none;
          }
        }

        :deep(.el-input__inner) {
          font-size: 16px;
          height: 48px;
        }
      }

      .el-button {
        width: 120px;
        height: 52px;
        font-size: 16px;
        border: 2px solid #409eff;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 5px;
      }
    }

    .result-section {
      margin-bottom: 40px;

      .result-card {
        background: linear-gradient(135deg, #3498db 0%, #2980b9 100%);
        border-radius: 16px;
        padding: 30px;
        color: #fff;
        text-align: center;
        box-shadow: 0 8px 20px rgba(52,152,219,0.3);

        .result-header {
          display: flex;
          align-items: center;
          justify-content: center;
          margin-bottom: 25px;

          .zodiac-icon {
            font-size: 60px;
            margin-right: 20px;
            animation: bounce 2s infinite;
          }

          .zodiac-info {
            text-align: left;

            .zodiac-title {
              font-size: 18px;
              opacity: 0.9;
              margin-bottom: 8px;
            }

            .zodiac-value {
              font-size: 36px;
              font-weight: bold;
              text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
            }
          }
        }

        .result-years {
          .years-title {
            font-size: 16px;
            opacity: 0.9;
            margin-bottom: 15px;
          }

          .years-list {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            justify-content: center;

            .year-tag {
              background: rgba(255,255,255,0.2);
              padding: 8px 16px;
              border-radius: 20px;
              font-size: 14px;
              backdrop-filter: blur(4px);
              transition: all 0.3s ease;

              &.current-year {
                background: #f1c40f;
                color: #2c3e50;
                font-weight: bold;
                transform: scale(1.1);
              }

              &:hover {
                background: rgba(255,255,255,0.3);
                transform: translateY(-2px);
              }
            }
          }
        }
      }
    }

    .zodiac-grid {
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      gap: 20px;
      margin-bottom: 40px;

      .zodiac-item {
        background: #f8fafc;
        border-radius: 16px;
        padding: 25px;
        text-align: center;
        transition: all 0.3s ease;
        cursor: pointer;
        border: 2px solid transparent;
        position: relative;
        overflow: hidden;

        &::before {
          content: '';
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          background: linear-gradient(135deg, rgba(52,152,219,0.1) 0%, rgba(52,152,219,0) 100%);
          opacity: 0;
          transition: opacity 0.3s ease;
        }

        &:hover {
          transform: translateY(-5px);
          box-shadow: 0 8px 20px rgba(0,0,0,0.1);

          &::before {
            opacity: 1;
          }

          .zodiac-icon {
            transform: scale(1.1);
          }
        }

        &.active {
          border-color: #3498db;
          background: #f0f7ff;

          .zodiac-icon {
            animation: bounce 2s infinite;
          }
        }

        .zodiac-icon {
          font-size: 48px;
          margin-bottom: 15px;
          transition: transform 0.3s ease;
        }

        .zodiac-info {
          .zodiac-name {
            font-size: 20px;
            color: #2c3e50;
            font-weight: 600;
            margin-bottom: 10px;
          }

          .zodiac-years {
            font-size: 13px;
            color: #666;
            line-height: 1.6;
          }
        }
      }
    }

    .help-text {
      background: #f8fafc;
      padding: 20px 25px;
      border-radius: 12px;
      margin-bottom: 30px;
      border-left: 4px solid #409eff;
      text-align: left;

      h3 {
        color: #2c3e50;
        margin: 0 0 15px 0;
        font-size: 18px;
        font-weight: 600;
      }

      .help-content {
        padding-left: 0;

        .help-item {
          color: #666;
          margin: 0 0 8px 0;
          line-height: 1.8;
          font-size: 15px;
        }

        ul {
          margin: 0 0 12px 20px;
          padding: 0;
          list-style: none;

          li {
            color: #666;
            line-height: 1.8;
            font-size: 14px;
            position: relative;
            margin-bottom: 6px;
            padding-left: 15px;

            &::before {
              content: '•';
              color: #409eff;
              position: absolute;
              left: 0;
              top: 0;
            }

            &:last-child {
              margin-bottom: 0;
            }
          }
        }
      }
    }

    .back-home {
      text-align: center;
      padding-top: 20px;
      border-top: 1px solid #ebeef5;

      .home-button {
        width: 200px;
        height: 44px;
        font-size: 16px;
        border-radius: 22px;
        background: linear-gradient(135deg, #409eff 0%, #3a8ee6 100%);
        border: none;
        color: white;
        transition: all 0.3s ease;

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(64,158,255,0.3);
        }

        .el-icon {
          margin-right: 6px;
        }
      }
    }
  }
}

@keyframes bounce {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-10px);
  }
}

@media screen and (max-width: 768px) {
  .chinese-zodiac {
    padding: 15px;

    h2 {
      font-size: 24px;
      margin-bottom: 30px;
    }

    .content-box {
      padding: 20px;

      .zodiac-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 15px;
      }

      .result-card {
        padding: 20px;

        .result-header {
          .zodiac-icon {
            font-size: 40px;
          }

          .zodiac-value {
            font-size: 30px;
          }
        }
      }

      .back-home {
        .home-button {
          width: 100%;
        }
      }

      .input-section {
        flex-direction: column;
        gap: 10px;

        .search-input {
          width: 100%;
        }

        .el-button {
          width: 100%;
        }
      }

      .help-text {
        padding: 15px 20px;

        h3 {
          font-size: 16px;
          margin-bottom: 12px;
        }

        .help-content {
          .help-item {
            font-size: 14px;
            margin-bottom: 6px;
          }

          ul {
            margin-left: 15px;
            margin-bottom: 10px;

            li {
              font-size: 13px;
              margin-bottom: 4px;
              padding-left: 12px;
            }
          }
        }
      }
    }
  }
}
</style> 